package com.wz.excelimport.controller;



import com.wz.excelimport.entity.ExcelEntity;
import com.wz.excelimport.entity.FuEntity;
import com.wz.excelimport.entity.ResResult;
import com.wz.excelimport.entity.UserEntity;
import com.wz.excelimport.service.impl.ExcelImportServiceImpl;
import com.wz.excelimport.utils.WriteExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 题库导入 后端控制器
 * </p>
 *
 * @author wz
 * @since 2018-11-27
 */
@Controller
@RequestMapping("/import")
@Validated
public class ExcelImportController {

    @Autowired
    private ExcelImportServiceImpl excelImportService;


    @GetMapping("index")
    public String index() {
        return "index";
    }


    @PostMapping("excel")
    @ResponseBody
    public ResResult singerQstImport(HttpServletRequest request,
            List<MultipartFile> file ){

        if (file.size() >= 2) {
            boolean a = false;
//            MultipartFile userFile = file.get(0);
//            String userFileName = file.get(0).getOriginalFilename();
//            MultipartFile jiangFile = file.get(1);
//            String jiangFileName = file.get(1).getOriginalFilename();

            try {

                List<FuEntity> fuEntities = null;
                List<UserEntity> userEntities = null;
                for (int i = 0; i < file.size(); i++) {
                    String fileName = file.get(i).getOriginalFilename();
                    if (fileName.contains("fu")) {
                        fuEntities = excelImportService.batchFuImport(fileName,file.get(i));
                    } else if (fileName.contains("user")) {
                        userEntities = excelImportService.batchUserImport(fileName,file.get(i));
                    }
                }


               List<ExcelEntity> excelEntities = new ArrayList<>();

                for (FuEntity entity:fuEntities) {
                    for (UserEntity userEntity:userEntities) {
                        if (userEntity.getOpenId().equals(entity.getOpenId())) {
                            ExcelEntity excelEntity = new ExcelEntity();
                            excelEntity.setFuName(entity.getFuName());
                            excelEntity.setOpenId(userEntity.getOpenId());
                            excelEntity.setPhone(userEntity.getPhone());
                            excelEntity.setUserName(userEntity.getUserName());
                            excelEntity.setWeixinname(userEntity.getWeixinname());
                            excelEntities.add(excelEntity);
                        }
                    }
                }


                List<Map> maps = new ArrayList<>();
                for (ExcelEntity excelEntity : excelEntities) {
                    HashMap<String, Object> stringObjectHashMap = new HashMap<>();
                    stringObjectHashMap.put("openId", excelEntity.getOpenId());
                    stringObjectHashMap.put("fuName", excelEntity.getFuName());
                    stringObjectHashMap.put("phone", excelEntity.getPhone());
                    stringObjectHashMap.put("username", excelEntity.getUserName());
                    stringObjectHashMap.put("weixinname", excelEntity.getWeixinname());
                    maps.add(stringObjectHashMap);
                }

                WriteExcel.writeExcel(maps,5,"/home/wz/11/test.xlsx");

                return ResResult.ok(excelEntities.size());

            } catch (Exception e) {
                return ResResult.errorMsg("导入失败");
            }
        }

        return null;

    }

}
